﻿using Dapper;
using System;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepReviewerConfig;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowTemplate.WorkflowTemplateStepReviewerConfig
{
	/// <summary>
	/// 工作流模板步骤审核人配置
	/// </summary>
	public class WorkflowTemplateStepReviewerConfigData : IWorkflowTemplateStepReviewerConfigData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowtemplatestepreviewerconfig";
		private const string QUERY = @"SELECT WorkflowTemplateStepReviewerConfigId,PermissionReferenceName,TemplateCode,TemplateName,TemplateStepCode,Id,`Key`,PermissionType,PermissionTypeStr,PermissionReferenceId FROM oa_workflowtemplatestepreviewerconfig";
		public WorkflowTemplateStepReviewerConfigData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作流模板步骤审核人配置是否存在
		public async Task<bool> Exists(long workflowTemplateStepReviewerConfigId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateStepReviewerConfigId=?WorkflowTemplateStepReviewerConfigId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepReviewerConfigId", workflowTemplateStepReviewerConfigId, DbType.Int64);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作流模板步骤审核人配置
		public async Task<WorkflowTemplateStepReviewerConfigEntity> Add(WorkflowTemplateStepReviewerConfigEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowtemplatestepreviewerconfig", new string[] { "PermissionReferenceName", "TemplateCode", "TemplateName", "TemplateStepCode", "Id", "`Key`", "PermissionType", "PermissionTypeStr", "PermissionReferenceId" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateStepReviewerConfigId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?PermissionReferenceName", entity.PermissionReferenceName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?TemplateStepCode", entity.TemplateStepCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?Id", entity.Id, DbType.Int32, size: 11);
			parameters.Add("?Key", entity.Key?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?PermissionType", entity.PermissionType?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?PermissionTypeStr", entity.PermissionTypeStr?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?PermissionReferenceId", entity.PermissionReferenceId?.Trim(), DbType.AnsiString, size: 50);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepReviewerConfigEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改工作流模板步骤审核人配置
		public async Task<WorkflowTemplateStepReviewerConfigEntity> Update(WorkflowTemplateStepReviewerConfigEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workflowtemplatestepreviewerconfig", new string[] { "PermissionReferenceName", "TemplateCode", "TemplateName", "TemplateStepCode", "Id", "`Key`", "PermissionType", "PermissionTypeStr", "PermissionReferenceId" }, new string[] { "WorkflowTemplateStepReviewerConfigId" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateStepReviewerConfigId=?WorkflowTemplateStepReviewerConfigId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepReviewerConfigId", entity.WorkflowTemplateStepReviewerConfigId, DbType.Int64, size: 20);
			parameters.Add("?PermissionReferenceName", entity.PermissionReferenceName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?TemplateStepCode", entity.TemplateStepCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?Id", entity.Id, DbType.Int32, size: 11);
			parameters.Add("?Key", entity.Key?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?PermissionType", entity.PermissionType?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?PermissionTypeStr", entity.PermissionTypeStr?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?PermissionReferenceId", entity.PermissionReferenceId?.Trim(), DbType.AnsiString, size: 50);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepReviewerConfigEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除工作流模板步骤审核人配置
		public async Task<bool> Delete(long workflowTemplateStepReviewerConfigId)
		{
			string sql = @"DELETE FROM oa_workflowtemplatestepreviewerconfig WHERE WorkflowTemplateStepReviewerConfigId=?WorkflowTemplateStepReviewerConfigId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepReviewerConfigId", workflowTemplateStepReviewerConfigId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		public async Task<bool> Delete(string templateCode)
		{
			string sql = @"DELETE FROM oa_workflowtemplatestepreviewerconfig WHERE TemplateCode=?TemplateCode";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?TemplateCode", templateCode, DbType.AnsiString, size: 50);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作流模板步骤审核人配置
		public async Task<WorkflowTemplateStepReviewerConfigEntity> GetEntity(long workflowTemplateStepReviewerConfigId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateStepReviewerConfigId=?WorkflowTemplateStepReviewerConfigId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepReviewerConfigId", workflowTemplateStepReviewerConfigId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateStepReviewerConfigEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板步骤审核人配置列表
		public async Task<IEnumerable<WorkflowTemplateStepReviewerConfigEntity>> GetList(string templateCode)
		{
			string sql = string.Format(@"{0} WHERE TemplateCode=?TemplateCode", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?TemplateCode", templateCode, DbType.AnsiString, size: 50);
			return await this.databaseContext.Slave.QueryAsync<WorkflowTemplateStepReviewerConfigEntity>(sql, parameters);
		}
		public async Task<IEnumerable<WorkflowTemplateStepReviewerConfigEntity>> GetListByTemplateStepCode(string templateStepCode)
		{
			string sql = string.Format(@"{0} WHERE TemplateStepCode=?TemplateStepCode", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?TemplateStepCode", templateStepCode, DbType.AnsiString, size: 50);
			return await this.databaseContext.Slave.QueryAsync<WorkflowTemplateStepReviewerConfigEntity>(sql, parameters);
		}
		
		#endregion

		#region 获取工作流模板步骤审核人配置分页
		public async Task<(IEnumerable<WorkflowTemplateStepReviewerConfigEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowtemplatestepreviewerconfig {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "WorkflowTemplateStepReviewerConfigId" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowTemplateStepReviewerConfigEntity, int>(sql, parameters);
		}
		#endregion
	}
}



